<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Account extends MY_Controller {

    public function __construct()
    {
        parent::__construct();
		$this->load->library('form_validation');
        $this->load->model('tank_auth/users','use');
        
    }

    /**
     * Default function
     */
    public function index($act=false)
    {
        if(!$this->tank_auth->is_logged_in())
            redirect('account/register');
        $this->load->model('orders');
        $profile = $this->use->get_profile($this->tank_auth->get_user_id());
        $this->assign('profile',$profile);
        $this->assign('orders',$this->orders->getOrder(false,$this->tank_auth->get_user_id()));
         //$post =$this->input->post(null);                
    }
    public function register(){
        
        
    }
    public function do_register(){
        $this->disable_view();
        $post =$this->input->post(null);
        global $site_info;
        $this->form_validation->set_rules('username', 'Username', 'required');
		$this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]');
		$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
		$this->form_validation->set_rules('email', 'Email', 'required|email');
        $this->form_validation->set_rules('name', 'Fullname', 'required');
        
        if ($this->form_validation->run() == FALSE)
		{
			_set_error_message(validation_errors());
            redirect('account/register');
		}
		else
		{
			$rs = $this->tank_auth->create_user($post['username'],$post['email'],$post['password'],false);
           
                        if(!empty($rs))
                        {
                            $this->load->model('tank_auth/roles');
                            $this->roles->set_roles($rs['user_id'],$site_info['user_role']);
                            $profile = array('user_id'=>$rs['user_id']
                            ,'country'=>$post['country'],'website'=>$post['website'],'name'=>$post['name']
                        ,'birthdate'=>strtotime($post['birthdate']),'gender'=>$post['gender'],'address'=>$post['address']
                        ,'favorite'=>$post['favorite'],'job'=>$post['job'],'company'=>$post['company'],'more'=>$post['more']);
                            $rs = $this->use->create_profile($profile);
                            _set_success_message('Save successful');
                        }
                        else
                            _set_error_message('....');
                        redirect('');
                        
		}
    }
    
    public function login(){ 
        if($this->tank_auth->is_logged_in())
            redirect('');
    }
  public function do_login()
  {
    $this->disable_view();
    
    $this->load->library('form_validation');
        $post = $this->input->post(null);
        if($this->tank_auth->is_logged_in())
            redirect('');
        $this->form_validation->set_rules('username', 'Username', 'required');
		$this->form_validation->set_rules('password', 'Password', 'required');
        if ($this->form_validation->run() == FALSE)
		{
			_set_error_message(validation_errors());
            redirect('account/login');
		}
		else
		{
            
                if($this->tank_auth->login($post['username'],$post['password'],false,true,false)){
                    if($post['returnurl']!=null)
                        redirect($post['returnurl']);
                    else
                    redirect();
                }
                else{
                    _set_error_message('Your username and password is incorrect. Please try again...');
                    
                    redirect('account/login');
                }
        }
    }
    
    public function forget_password(){
        
    }
    function reset_password($user_id,$key){
        
    }
    public function send_password()
    {
        
        $post = $this->input->post(null);
      
        if(!$post['email']){
            _set_error_message('Please fill email address');
            redirect('account/forget_password');
        }
       
        if($this->tank_auth->is_email_available($post['email']))
        {
            $data = $this->tank_auth->forgot_password($post['email']);
            
            if($this->_send_mail('forgot-password',$post['email'],$data)){
                $data = 'Ðã gửi email reset password';
            }else{
                $data = 'Không thể gửi mail';
            }
        }else{
            $data ='Không tìm thấy trong CSDL';
        }
        $this->assign('data',$data);
        redirect();    
        
    }
    
    public function can_reset_password(){
        $this->disable_view();
        $post = $this->input->post(null);
        $this->form_validation->set_rules('password', 'New Password', 'required|matches[passconf]');
		$this->form_validation->set_rules('passconf', 'Confirm new password', 'required');
        if ($this->form_validation->run() == FALSE)
		{
		  
		}else{
		  if($this->tank_auth->can_reset_password($post['user_id'],$post['key'])){
		      $this->tank_auth->reset_password($post['user_id'],$post['key'],$post['password']);
              $data['password'] = $post['password'];
            
              $this->_send_mail('forgot-password','harry88pham@gmail.com',$data);
             redirect('');
		  }
		}
    }
    
    public function _send_mail($type,$email,$data){
        $this->disable_view();
        $config['protocol']='smtp';  
        $config['smtp_host']='ssl://smtp.gmail.com';  
        $config['smtp_port']='465';  
        $config['smtp_timeout']='3000';  
        $config['smtp_user']='harry88pham@gmail.com';  
        $config['smtp_pass']='05111988';  
        $config['charset']='utf-8';  
        $config['newline']="\r\n"; 
        $config['mailtype'] = 'html'; 
        $config['wordwrap'] = FALSE;
        
        $this->load->library('email',$config);
        $this->email->from('harry88pham@gmail.com');
              $this->email->to($email);

              $this->email->subject('tesstng');
              $this->email->message($this->load->view('layouts/email/'.$type.'-html',$data,true));  
        $this->email->send();
        
      
    }
    public function update(){
        $this->disable_view();
        $post = $this->input->post(null,true);
       $row = $this->use->update_profile(array('name'=>$post['name']
                                        
                                        ,'gender'=>$post['gender']
                                        ,'address'=>$post['address']
                                        ,'country'=>$post['country']
                                        ,'favorite'=>$post['favorite']
                                        ,'job'=>$post['job']
                                        ,'company'=>$post['company']
                                        ,'website'=>$post['website']
                                        ,'yahoo'=>$post['yahoo']
                                        ,'cmnd'=>$post['cmnd']
                                        ,'more'=>$post['more']),array('user_id'=>$this->tank_auth->get_user_id()));
redirect('account');
    }
    public function logout(){
        $this->disable_view();
        $this->tank_auth->logout();
        redirect(''); 
    }
    
    
    
}